package com.blog.mapper;

import com.blog.entity.Category;
import com.blog.entity.Works;
import com.blog.vo.CategoryVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface WorksMapper {

    @Select("select * from tb_works where user_id = #{currentId} and is_show =#{isShow}")
    List<Works> selectWorksIsShow(@Param("currentId") Long currentId,@Param("isShow")  Long isShow);
    @Select("select * from tb_works where user_id = #{Uid}")
    List<Works> selectById(@Param("Uid") Long Uid);
    @Select("select * from tb_works where user_id = #{Uid} and is_show = #{isShow}")
    List<Works> userSelectById(@Param("Uid") Long Uid,@Param("isShow") Long isShow);

    @Select("select * from tb_works where user_id = #{currentId} and id = #{wid}")
    Works selectByIdAndWid(@Param("wid") Long wid, @Param("currentId") Long currentId);
    @Select("select * from tb_works where id = #{wid}")
    Works selectOneById(Long wid);
    
    @Select("select * from tb_works where user_id = #{currentId} and title LIKE CONCAT('%', #{value}, '%')")
    List<Works> selectByIdAndName(@Param("value") String value, @Param("currentId") Long currentId);

    @Insert("insert into tb_works(cover,title, info, file_path, create_time, user_id) values (#{cover},#{title},#{info},#{filePath},#{createTime},#{userId})")
    void insert(Works works);

    void update(Works works);

    @Delete("delete from tb_works where id = #{id}")
    void deleteById(@Param("id") Long id);

    @Select("SELECT * FROM tb_works WHERE id in (SELECT works_id FROM tb_works_category WHERE category_id = #{categoryId}) and user_id = #{currentId}")
    List<Works> worksByCategory(@Param("categoryId") Long categoryId, @Param("currentId") Long currentId);

    @Select("SELECT * FROM tb_works as w WHERE w.id NOT IN (SELECT c.works_id FROM tb_works_category as c ) and user_id = #{currentId}")
    List<Works> worksByNoCategory(@Param("currentId") Long currentId);

    @Select("SELECT * FROM tb_category " +
            "WHERE id NOT IN (SELECT category_id FROM tb_works_category WHERE works_id = #{worksId}) " +
            "AND id IN (SELECT category_id FROM tb_user_category WHERE user_id = #{currentId});")
    List<Category> getNoCategory(@Param("worksId") Long worksId, @Param("currentId") Long currentId);

    @Select("SELECT * FROM tb_category " +
            "WHERE id IN (SELECT category_id FROM tb_works_category WHERE works_id = #{worksId}) " +
            "AND id IN (SELECT category_id FROM tb_user_category WHERE user_id = #{currentId});")
    List<Category> getCategory(@Param("worksId") Long worksId, @Param("currentId") Long currentId);


    @Select("SELECT *  FROM tb_category WHERE id in (SELECT category_id FROM tb_works_category WHERE works_id = #{worksId})")
    List<CategoryVO> getCategoryByWorks(@Param("worksId") Long wid);

    @Update("update tb_works set  is_show = #{isShow} where id = #{worksId}")
    void updateIsShow(@Param("worksId") Long worksId,@Param("isShow") Long isShow);


    @Select("SELECT count(*) FROM tb_works WHERE user_id = #{uid}")
    Long totalWorksByUid(Long uid);
}
